package com.xiaotu.code.service.quartz;

import java.util.List;

import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaotu.code.dao.quartz.QuartzCronExpressionDao;
import com.xiaotu.code.model.quartz.QuartzCronExpression;
import com.xiaotu.code.system.ServiceException;

/**
 * 定时器时间配置.
 * 
 * @author chenxiangrong
 */
@Service("quartzCronExpressionService")
@Transactional(rollbackFor = Exception.class)
public class QuartzCronExpressionServiceImpl implements QuartzCronExpressionService, BeanFactoryAware {

	private static final long serialVersionUID = -1777187896431971839L;
	private static final String SCHEDULER_FACTORY_BEAN = "schedulerFactoryBean";

	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	private BeanFactory beanFactory;
	@Autowired
	private QuartzCronExpressionDao quartzCronExpressionDao;

	@Override
	public List<QuartzCronExpression> getAllQuartzCronExpression() throws ServiceException {

		try {
			return quartzCronExpressionDao.getAll();
		} catch (Exception e) {
			logger.error("获取所有定时器时间配置", e);
			throw new ServiceException("获取所有定时器时间配置", e);
		}
	}

	@Override
	public void reSetScheduleJob(QuartzCronExpression qce) throws ServiceException {

		Scheduler scheduler = (Scheduler) this.beanFactory.getBean(SCHEDULER_FACTORY_BEAN);

		try {

			CronTriggerBean trigger = (CronTriggerBean) scheduler
					.getTrigger(qce.getBeanName(), Scheduler.DEFAULT_GROUP);

			int triggerState = scheduler.getTriggerState(qce.getBeanName(), Scheduler.DEFAULT_GROUP);
			// 如果是要求关闭任务，则停止服务器
			if (STOP_STATUS.equals(qce.getStatus())) {
				if (Trigger.STATE_PAUSED != triggerState) {
					scheduler.pauseTrigger(qce.getBeanName(), Scheduler.DEFAULT_GROUP);
				}
			} else {
				// 之前是关闭的，或者时间改变，重新启动任务
				String originConExpression = trigger.getCronExpression();
				if (!originConExpression.equals(qce.getExpression()) || Trigger.STATE_PAUSED == triggerState) {
					trigger.setCronExpression(qce.getExpression());
					scheduler.rescheduleJob(qce.getBeanName(), Scheduler.DEFAULT_GROUP, trigger);
				}
			}
		} catch (Exception e) {
			logger.error("重新设置定时器时间配置", e);
			throw new ServiceException("重新设置定时器时间配置", e);
		}
	}

	@Override
	public QuartzCronExpression getQuartzCronExpression(String beanName) throws ServiceException {

		try {
			return quartzCronExpressionDao.findQuartzCronExpression(beanName);
		} catch (Exception e) {
			logger.error("获取定时器时间配置", e);
			throw new ServiceException("获取定时器时间配置", e);
		}
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

}
